#!/usr/bin/python
import _acquirepy as acquirepy
import math
import os, time
import tempfile, shutil
import test_credentials

# PYTHONPATH=<path to buid directory> nosetests 

# nosetest found that the system does not tolerate setup/teardown cycles

def setup():
   acquirepy.acquireCommonInitLibraries()

def teardown():
   acquirepy.acquireCommonDestroy()

def test_inituserstate():
   retcode = acquirepy.acquireClientInitState(test_credentials.test_broker, 10000, test_credentials.test_username, test_credentials.test_username, test_credentials.test_password, 'broker')
   assert(retcode != 0)
   acquirepy.acquireCommonDestroyState()

def test_userregistration():
   acquirepy.acquireCommonDestroyState()
   retcode = acquirepy.acquireClientInitState(test_credentials.test_broker, 10000, test_credentials.test_username, test_credentials.test_username, test_credentials.test_password, 'broker')
   assert(retcode != 0)
   retcode = acquirepy.acquireClientRegisterUserWithBroker()
   assert(retcode != 3)
   acquirepy.acquireCommonDestroyState()

def test_sshadd1():
   acquirepy.acquireCommonDestroyState()
   if (test_credentials.ssh_test_machine1 == '') or (test_credentials.ssh_test_machine1 == None):
      return
   retcode = acquirepy.acquireClientInitState(test_credentials.test_broker, 10000, test_credentials.test_username, test_credentials.test_username, test_credentials.test_password, 'broker')
   assert(retcode != 0)
   retcode = acquirepy.acquireClientRegisterUserWithBroker()
   assert(retcode != 3)
   retval = acquirepy.acquireClientAddSSHHost('%s@%s' % (test_credentials.ssh_test_username1, test_credentials.ssh_test_machine1), test_credentials.ssh_test_password1, None, None)
   assert(retcode == 1)
   acquirepy.acquireCommonDestroyState()

def test_sshadd2():
   acquirepy.acquireCommonDestroyState()
   if (test_credentials.ssh_test_machine2 == '') or (test_credentials.ssh_test_machine2 == None):
      return
   retcode = acquirepy.acquireClientInitState(test_credentials.test_broker, 10000, test_credentials.test_username, test_credentials.test_username, test_credentials.test_password, 'broker')
   assert(retcode != 0)
   retcode = acquirepy.acquireClientRegisterUserWithBroker()
   assert(retcode != 3)
   retval = acquirepy.acquireClientAddSSHHost('%s@%s' % (test_credentials.ssh_test_username2, test_credentials.ssh_test_machine2), test_credentials.ssh_test_password2, None, None)
   assert(retcode == 1)
   acquirepy.acquireCommonDestroyState()
   
def test_userregistrationwasok():
   acquirepy.acquireCommonDestroyState()
   retcode = acquirepy.acquireClientInitState(test_credentials.test_broker, 10000, test_credentials.test_username, test_credentials.test_username, test_credentials.test_password, 'broker')
   assert(retcode != 0)
   retcode = acquirepy.acquireClientRegisterUserWithBroker()
   assert(retcode == 1)
   acquirepy.acquireCommonDestroyState()

def test_createdatastore():
   blocksize = 1048576
   acquirepy.acquireCommonDestroyState()
   retcode = acquirepy.acquireClientInitState(test_credentials.test_broker, 10000, test_credentials.test_username, test_credentials.test_username, test_credentials.test_password, 'broker')
   assert(retcode != 0)
   retcode = acquirepy.acquireClientRegisterUserWithBroker()
   assert(retcode == 1)
   work_datastore_info = acquirepy.acquirePythonCreateDataStore("../test_workpacket", None, None, 1, blocksize)
   assert(acquirepy.acquirePythonGetDSIsValid(work_datastore_info))
   acquirepy.acquireCommonDestroyState()

def test_checkforblankdatastore():
   blocksize = 1048576
   acquirepy.acquireCommonDestroyState()
   retcode = acquirepy.acquireClientInitState(test_credentials.test_broker, 10000, test_credentials.test_username, test_credentials.test_username, test_credentials.test_password, 'broker')
   assert(retcode != 0)
   retcode = acquirepy.acquireClientRegisterUserWithBroker()
   assert(retcode == 1)
   user_resname = acquirepy.acquireCommonResolve(test_credentials.test_username)
   assert(user_resname != None)
   res = acquirepy.acquireCommonResolve(user_resname + ".childrennames")
   assert(res != None)
   resstack = []
   splitres = res.split(",")
   found_empty_child_datastore = 0
   for child in splitres:
      fullname = user_resname + "." + child
      fullname_progress = acquirepy.acquireCommonResolve(fullname + ".progress")
      if (math.fabs(float(fullname_progress)) == 0.0):
         found_empty_child_datastore = 1
   assert(found_empty_child_datastore == 1)
   acquirepy.acquireCommonDestroyState()

def test_createuploaddatastore():
   blocksize = 1048576
   acquirepy.acquireCommonDestroyState()
   retcode = acquirepy.acquireClientInitState(test_credentials.test_broker, 10000, test_credentials.test_username, test_credentials.test_username, test_credentials.test_password, 'broker')
   assert(retcode != 0)
   retcode = acquirepy.acquireClientRegisterUserWithBroker()
   assert(retcode == 1)
   work_datastore_info = acquirepy.acquirePythonCreateDataStore("../test_workpacket", None, None, 1, blocksize)
   assert(acquirepy.acquirePythonGetDSIsValid(work_datastore_info))
   work_datastore_name = acquirepy.acquirePythonGetDSName(work_datastore_info)
   work_datastore_blockcount = acquirepy.acquirePythonGetDSBlockCount(work_datastore_info)
   keyname_encrypting = '%s_inp' % work_datastore_name
   retcode = acquirepy.acquirePythonUploadToDataStore(work_datastore_name, "../test_workpacket", blocksize, keyname_encrypting)
   assert(retcode == 0)
   acquirepy.acquireCommonDestroyState()

def test_checkforcompleteddatastore():
   blocksize = 1048576
   acquirepy.acquireCommonDestroyState()
   retcode = acquirepy.acquireClientInitState(test_credentials.test_broker, 10000, test_credentials.test_username, test_credentials.test_username, test_credentials.test_password, 'broker')
   assert(retcode != 0)
   retcode = acquirepy.acquireClientRegisterUserWithBroker()
   assert(retcode == 1)
   user_resname = acquirepy.acquireCommonResolve(test_credentials.test_username)
   assert(user_resname != None)
   res = acquirepy.acquireCommonResolve(user_resname + ".childrennames")
   assert(res != None)
   resstack = []
   splitres = res.split(",")
   found_complete_child_datastore = 0
   for child in splitres:
      fullname = user_resname + "." + child
      fullname_progress = acquirepy.acquireCommonResolve(fullname + ".progress")
      if (math.fabs(float(fullname_progress)) == 1.0):
         found_complete_child_datastore = 1
   assert(found_complete_child_datastore == 1)
   acquirepy.acquireCommonDestroyState()

def test_createuploadpurposedatastore():
   blocksize = 1048576
   acquirepy.acquireCommonDestroyState()
   retcode = acquirepy.acquireClientInitState(test_credentials.test_broker, 10000, test_credentials.test_username, test_credentials.test_username, test_credentials.test_password, 'broker')
   assert(retcode != 0)
   retcode = acquirepy.acquireClientRegisterUserWithBroker()
   assert(retcode == 1)
   work_datastore_info = acquirepy.acquirePythonCreateDataStore("../test_workpacket", None, None, 1, blocksize)
   assert(acquirepy.acquirePythonGetDSIsValid(work_datastore_info))
   work_datastore_name = acquirepy.acquirePythonGetDSName(work_datastore_info)
   work_datastore_blockcount = acquirepy.acquirePythonGetDSBlockCount(work_datastore_info)
   keyname_encrypting = '%s_inp' % work_datastore_name
   retcode = acquirepy.acquirePythonUploadToDataStore(work_datastore_name, "../test_workpacket", blocksize, keyname_encrypting)
   assert(retcode == 0)
   #time.sleep(test_credentials.sleep_remote_ssh_key_reload)
   retcode = acquirepy.acquireCommonSetPurposeFlagsOnDataStore(work_datastore_name, 1, 0, None, 60 * 60 * 24, 1, test_credentials.test_password)
   assert(retcode == 1)
   acquirepy.acquireCommonDestroyState()

def test_checkforworkinstance():
   blocksize = 1048576
   acquirepy.acquireCommonDestroyState()
   retcode = acquirepy.acquireClientInitState(test_credentials.test_broker, 10000, test_credentials.test_username, test_credentials.test_username, test_credentials.test_password, 'broker')
   assert(retcode != 0)
   retcode = acquirepy.acquireClientRegisterUserWithBroker()
   assert(retcode == 1)
   user_resname = acquirepy.acquireCommonResolve(test_credentials.test_username)
   assert(user_resname != None)
   res = acquirepy.acquireCommonResolve(user_resname + ".childrennames")
   assert(res != None)
   resstack = []
   splitres = res.split(",")
   found_workinstance = 0
   for child in splitres:
      fullname = user_resname + "." + child
      fullname_workinstance = acquirepy.acquireCommonResolve(fullname + ".instance1")
      if not ((fullname_workinstance == '') or (fullname_workinstance == None)):
         found_workinstance = 1
   assert(found_workinstance == 1)
   acquirepy.acquireCommonDestroyState()

def test_waitforworktocomplete():
   blocksize = 1048576
   acquirepy.acquireCommonDestroyState()
   retcode = acquirepy.acquireClientInitState(test_credentials.test_broker, 10000, test_credentials.test_username, test_credentials.test_username, test_credentials.test_password, 'broker')
   assert(retcode != 0)
   retcode = acquirepy.acquireClientRegisterUserWithBroker()
   assert(retcode == 1)
   user_resname = acquirepy.acquireCommonResolve(test_credentials.test_username)
   assert(user_resname != None)
   res = acquirepy.acquireCommonResolve(user_resname + ".childrennames")
   assert(res != None)
   resstack = []
   splitres = res.split(",")
   found_workinstance = 0
   workinstance_fullname = ''
   for child in splitres:
      fullname = user_resname + "." + child
      fullname_workinstance = fullname + ".instance1"
      resolvedname_workinstance = acquirepy.acquireCommonResolve(fullname_workinstance)
      if not ((resolvedname_workinstance == '') or (resolvedname_workinstance == None)):
         found_workinstance = 1
         workinstance_fullname = fullname_workinstance
   assert(found_workinstance == 1)
   workinstance_complete = 0
   time_seconds_count = 0
   while (workinstance_complete == 0):
      time.sleep(1)
      resolved_workinstance_progress = acquirepy.acquireCommonResolve(workinstance_fullname + ".progress")
      if (float(resolved_workinstance_progress) == 1.0):
         workinstance_complete = 1
      time_seconds_count = time_seconds_count + 1
      assert(time_seconds_count < test_credentials.wait_for_running_timeout)
   assert(workinstance_complete == 1)

def test_findresultsdatastore():
   time.sleep(test_credentials.sleep_remote_create_results)
   blocksize = 1048576
   acquirepy.acquireCommonDestroyState()
   retcode = acquirepy.acquireClientInitState(test_credentials.test_broker, 10000, test_credentials.test_username, test_credentials.test_username, test_credentials.test_password, 'broker')
   assert(retcode != 0)
   retcode = acquirepy.acquireClientRegisterUserWithBroker()
   assert(retcode == 1)
   user_resname = acquirepy.acquireCommonResolve(test_credentials.test_username)
   assert(user_resname != None)
   res = acquirepy.acquireCommonResolve(user_resname + ".childrennames")
   assert(res != None)
   resstack = []
   splitres = res.split(",")
   found_results = 0
   for child in splitres:
      fullname = user_resname + "." + child
      fullname_workinstance = fullname + ".instance1"
      resolvedname_workinstance = acquirepy.acquireCommonResolve(fullname_workinstance)
      if not ((resolvedname_workinstance == '') or (resolvedname_workinstance == None)):
         chnames = acquirepy.acquireCommonResolve(fullname_workinstance + ".childrennames")
         if not ((chnames == '') or (chnames == None)):
            splitchnames = chnames.split(",")
            for ch in splitchnames:
               if not ((ch == '') or (ch == None)):
                  found_results = 1
   assert(found_results == 1)
   acquirepy.acquireCommonDestroyState()

def test_waitforresultstoupload():
   blocksize = 1048576
   acquirepy.acquireCommonDestroyState()
   retcode = acquirepy.acquireClientInitState(test_credentials.test_broker, 10000, test_credentials.test_username, test_credentials.test_username, test_credentials.test_password, 'broker')
   assert(retcode != 0)
   retcode = acquirepy.acquireClientRegisterUserWithBroker()
   assert(retcode == 1)
   user_resname = acquirepy.acquireCommonResolve(test_credentials.test_username)
   assert(user_resname != None)
   res = acquirepy.acquireCommonResolve(user_resname + ".childrennames")
   assert(res != None)
   resstack = []
   splitres = res.split(",")
   found_progressable_results = 0
   for child in splitres:
      fullname = user_resname + "." + child
      fullname_workinstance = fullname + ".instance1"
      resolvedname_workinstance = acquirepy.acquireCommonResolve(fullname_workinstance)
      if not ((resolvedname_workinstance == '') or (resolvedname_workinstance == None)):
         chnames = acquirepy.acquireCommonResolve(fullname_workinstance + ".childrennames")
         if not ((chnames == '') or (chnames == None)):
            splitchnames = chnames.split(",")
            for ch in splitchnames:
               if not ((ch == '') or (ch == None)):
                  fullname_results_workinstance = fullname_workinstance + "." + ch
                  resolvedname_results_workinstance = acquirepy.acquireCommonResolve(fullname_results_workinstance)
                  if not ((resolvedname_results_workinstance == '') or (resolvedname_results_workinstance == None)):
                     fullname_results_workinstance_progress = fullname_results_workinstance + ".progress"
                     found_progressable_results = 1
   assert(found_progressable_results == 1)
   results_uploaded = 0
   time_seconds_count = 0
   while (results_uploaded == 0):
      time.sleep(1)
      resolved_upload_progress = acquirepy.acquireCommonResolve(fullname_results_workinstance_progress)
      if (float(resolved_upload_progress) == 1.0):
         results_uploaded = 1
      time_seconds_count = time_seconds_count + 1
      assert(time_seconds_count < test_credentials.wait_for_result_upload)
   assert(results_uploaded == 1)
   acquirepy.acquireCommonDestroyState()

def test_checkanyresultdatastoreisvalid():
   blocksize = 1048576
   acquirepy.acquireCommonDestroyState()
   retcode = acquirepy.acquireClientInitState(test_credentials.test_broker, 10000, test_credentials.test_username, test_credentials.test_username, test_credentials.test_password, 'broker')
   assert(retcode != 0)
   retcode = acquirepy.acquireClientRegisterUserWithBroker()
   assert(retcode == 1)
   user_resname = acquirepy.acquireCommonResolve(test_credentials.test_username)
   assert(user_resname != None)
   res = acquirepy.acquireCommonResolve(user_resname + ".childrennames")
   assert(res != None)
   resstack = []
   splitres = res.split(",")
   found_downloadable_results = 0
   for child in splitres:
      fullname = user_resname + "." + child
      fullname_workinstance = fullname + ".instance1"
      resolvedname_workinstance = acquirepy.acquireCommonResolve(fullname_workinstance)
      if not ((resolvedname_workinstance == '') or (resolvedname_workinstance == None)):
         chnames = acquirepy.acquireCommonResolve(fullname_workinstance + ".childrennames")
         if not ((chnames == '') or (chnames == None)):
            splitchnames = chnames.split(",")
            for ch in splitchnames:
               if not ((ch == '') or (ch == None)):
                  fullname_results_workinstance = fullname_workinstance + "." + ch
                  resolvedname_results_workinstance = acquirepy.acquireCommonResolve(fullname_results_workinstance)
                  if not ((resolvedname_results_workinstance == '') or (resolvedname_results_workinstance == None)):
                     results_datastore_info = acquirepy.acquireCommonGetDataStoreInformation(resolvedname_results_workinstance)
                     assert(acquirepy.acquirePythonGetDSIsValid(results_datastore_info))
   acquirepy.acquireCommonDestroyState()

def test_downloadcompleteresults():
   blocksize = 1048576
   acquirepy.acquireCommonDestroyState()
   retcode = acquirepy.acquireClientInitState(test_credentials.test_broker, 10000, test_credentials.test_username, test_credentials.test_username, test_credentials.test_password, 'broker')
   assert(retcode != 0)
   retcode = acquirepy.acquireClientRegisterUserWithBroker()
   assert(retcode == 1)
   user_resname = acquirepy.acquireCommonResolve(test_credentials.test_username)
   assert(user_resname != None)
   res = acquirepy.acquireCommonResolve(user_resname + ".childrennames")
   assert(res != None)
   resstack = []
   splitres = res.split(",")
   found_downloadable_results = 0
   for child in splitres:
      fullname = user_resname + "." + child
      fullname_workinstance = fullname + ".instance1"
      resolvedname_workinstance = acquirepy.acquireCommonResolve(fullname_workinstance)
      if not ((resolvedname_workinstance == '') or (resolvedname_workinstance == None)):
         chnames = acquirepy.acquireCommonResolve(fullname_workinstance + ".childrennames")
         if not ((chnames == '') or (chnames == None)):
            splitchnames = chnames.split(",")
            for ch in splitchnames:
               if not ((ch == '') or (ch == None)):
                  fullname_results_workinstance = fullname_workinstance + "." + ch
                  resolvedname_results_workinstance = acquirepy.acquireCommonResolve(fullname_results_workinstance)
                  if not ((resolvedname_results_workinstance == '') or (resolvedname_results_workinstance == None)):
                     fullname_results_workinstance_progress = fullname_results_workinstance + ".progress"
                     resolved_upload_progress = acquirepy.acquireCommonResolve(fullname_results_workinstance_progress)
                     if (float(resolved_upload_progress) == 1.0):
                        found_downloadable_results = 1
   assert(found_downloadable_results == 1)
   results_datastore_info = acquirepy.acquireCommonGetDataStoreInformation(resolvedname_results_workinstance)
   assert(acquirepy.acquirePythonGetDSIsValid(results_datastore_info))
   decryptionkey = acquirepy.acquirePythonGetDSDecryptionKey(results_datastore_info)
   blockcount = acquirepy.acquirePythonGetDSBlockCount(results_datastore_info)
   blocksize = acquirepy.acquirePythonGetDSBlockSize(results_datastore_info)
   instancecount = acquirepy.acquirePythonGetDSInstanceCount(results_datastore_info)
   isworkpacket = acquirepy.acquirePythonGetDSIsWorkPacket(results_datastore_info)
   isresults = acquirepy.acquirePythonGetDSIsResults(results_datastore_info)
   iscomplete = acquirepy.acquirePythonGetDSIsComplete(results_datastore_info)
   assert(isresults)
   assert(iscomplete)
   temp_directory = tempfile.mkdtemp()
   temp_results_directory = temp_directory + "/results"
   os.mkdir(temp_results_directory)
   assert(os.path.exists(temp_results_directory) == 1)
   acquirepy.acquireCommonDeferredDownloadFromDataStore(resolvedname_results_workinstance, temp_directory, blockcount, None)
   assert(os.path.exists(temp_directory + "/datastore_" + resolvedname_results_workinstance + ".block0") == 1)
   acquirepy.acquireCommonReconstructDeferredDownload(resolvedname_results_workinstance, temp_directory, temp_results_directory, blockcount, decryptionkey)
   assert(os.path.exists(temp_results_directory + "/work_completed.txt") == 1)
   shutil.rmtree(temp_directory)
   acquirepy.acquireCommonDestroyState()
